package com.zzedu.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.zzedu.common.enums.DeletedEnums;
import com.zzedu.common.utils.bean.BeanUtils;
import com.zzedu.common.utils.security.LoginUser;
import com.zzedu.domain.ZzClasses;
import com.zzedu.manager.IZzStuClassesManager;
import com.zzedu.result.Result;
import com.zzedu.result.ResultEnum;
import com.zzedu.service.IZzClassesService;
import com.zzedu.service.IZzZzStuClassesRService;
import com.zzedu.stu.rsp.ApiZzClassesInfoVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class ZzStuClassesManagerImpl implements IZzStuClassesManager {

    Logger log = LoggerFactory.getLogger(IZzStuClassesManager.class);
    @Autowired
    IZzZzStuClassesRService zzStuClassesRService;

    @Autowired
    IZzClassesService zzClassesService;

    @Override
    public Result<List<ApiZzClassesInfoVO>> getLoginClassList(LoginUser loginUser) {
        LambdaQueryWrapper<ZzClasses> lambdaQueryWrapper = Wrappers.lambdaQuery(ZzClasses.class);
        lambdaQueryWrapper.apply("id in(select class_id from zz_stu_classes_r where stu_id={0} and deleted={1})", loginUser.getUserId(), DeletedEnums.NO.getCode());
        lambdaQueryWrapper.apply("school_id in(select id from zz_school where deleted={0})", DeletedEnums.NO.getCode());
        Result<List<ApiZzClassesInfoVO>> result = new Result<>();
        result.setResultCode(ResultEnum.SUCCESS.getCode());
        result.setResultMessage(ResultEnum.SUCCESS.getDesc());
        result.setData(Optional.ofNullable(zzClassesService.list(lambdaQueryWrapper)).orElse(Lists.newArrayList()).stream().map(clazz -> {
            ApiZzClassesInfoVO apiZzClassesInfoVO = new ApiZzClassesInfoVO();
            BeanUtils.copyBeanProp(apiZzClassesInfoVO, clazz);
            return apiZzClassesInfoVO;
        }).collect(Collectors.toList()));
        return result;

    }
}
